﻿Public Class frmMPersona
    Public modeForm As Integer
    Public frmParent As frmPersona

    Private Sub frmRPersona_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.btnCancelar.Text = "Cancelar"
        Me.btnGuardar.Visible = True

        Select Case Me.modeForm
            Case 1 'Nuevo
                Me.txtCodigo.Text = ""
                Me.txtCedula.Text = ""
                Me.txtSnombre.Text = ""
                Me.txtPnombre.Text = ""
                Me.txtPapellido.Text = ""
                Me.txtSapellido.Text = ""
                Me.dtpFechanacimiento.Text = ""
                'Me.cmbCiudad.SelectedValue = ""
                'Me.cmbAlias.SelectedValue = ""
                'Me.cmbSexo.SelectedValue = ""
                'Me.cmbTipodocumento.SelectedValue = ""
                'Me.cmbTiposangre.SelectedValue = ""


            Case 2 'Consultar
                Me.txtCodigo.Text = Me.frmParent.arrConsulta(0)
                Me.txtCedula.Text = Me.frmParent.arrConsulta(6)
                Me.txtPnombre.Text = Me.frmParent.arrConsulta(7)
                Me.txtSnombre.Text = Me.frmParent.arrConsulta(8)
                Me.txtPapellido.Text = Me.frmParent.arrConsulta(9)
                Me.txtSapellido.Text = Me.frmParent.arrConsulta(10)
                Me.dtpFechanacimiento.Text = Me.frmParent.arrConsulta(15)

                If Me.frmParent.arrConsulta(1) <> "" Then
                    Me.cmbTipodocumento.SelectedValue = Me.frmParent.arrConsulta(1)
                End If
                If Me.frmParent.arrConsulta(3) <> "" Then
                    Me.cmbCiudad.SelectedValue = Me.frmParent.arrConsulta(3)
                End If
                If Me.frmParent.arrConsulta(4) <> "" Then
                    Me.cmbSexo.SelectedValue = Me.frmParent.arrConsulta(4)
                End If
                If Me.frmParent.arrConsulta(5) <> "" Then
                    Me.cmbTiposangre.SelectedValue = Me.frmParent.arrConsulta(5)
                End If

                Me.btnCancelar.Text = "Continuar"
                Me.btnGuardar.Visible = False

            Case 3 'Editar
                Dim dstRecord As New DataSet
                Dim strId As String = Me.frmParent.dgvPersona.Rows(Me.frmParent.dgvPersona.CurrentRow.Index).Cells(0).Value.ToString

                dstRecord.Tables.Add("persona")
                mdlCRUD.loadRecord(dstRecord, "persona", "persona", "persona_id = '" & strId & "'", Nothing)

                Dim arrRow As Array
                arrRow = dstRecord.Tables("persona").Rows(0).ItemArray.ToArray

                Me.lblEdCodigo.Text = arrRow(0).ToString
                Me.txtCodigo.Text = arrRow(0).ToString
                Me.txtCedula.Text = arrRow(1).ToString
                Me.txtPnombre.Text = arrRow(3).ToString
                Me.txtSnombre.Text = arrRow(4).ToString
                Me.txtPapellido.Text = arrRow(5).ToString
                Me.txtSapellido.Text = arrRow(6).ToString
                Me.dtpFechanacimiento.Text = arrRow(7).ToString

                If arrRow(2).ToString <> "" Then
                    Me.cmbTipodocumento.SelectedValue = arrRow(2)
                End If
                If arrRow(11).ToString <> "" Then
                    Me.cmbSexo.SelectedValue = arrRow(11).ToString
                End If
                If arrRow(9).ToString <> "" Then
                    Me.cmbTiposangre.SelectedValue = arrRow(9).ToString
                End If
                'dstRecord1.Tables.Add("equipopersona")
                'mdlCRUD.loadRecord(dstRecord1, "equipopersona", "equipopersona", "persona_id = '" & strId & "'", Nothing)
                'If (dstRecord1.Tables("equipopersona").Rows.Count > 0) Then
                '    For i = 0 To dstRecord1.Tables("equipopersona").Rows.Count - 1
                '        arrRow1 = dstRecord1.Tables("equipopersona").Rows(i).ItemArray.ToArray
                '        dstRecord1.Tables.Add("equipo")
                '        mdlCRUD.loadRecord(dstRecord1, "equipo", "equipo", "equipo_id = '" & arrRow1(0) & "'", Nothing)
                '        arrRow2 = dstRecord1.Tables("equipo").Rows(0).ItemArray.ToArray
                '        Me.dgvEquipo.Rows(i).Cells(0).Value = arrRow1(0).ToString
                '        Me.dgvEquipo.Rows(i).Cells(2).Value = arrRow2(1).ToString
                '        Me.dgvEquipo.Rows(i).Cells(3).Value = arrRow1(2).ToString
                '    Next i
                'End If

        End Select
    End Sub

    Private Sub btnGuardar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGuardar.Click
        Select Case Me.modeForm
            Case 1 'Nuevo
                Dim arrCampos() As String = {
                                "persona_id",
                                "tipodocumento_id",
                                "persona_lugarnacimiento",
                                "sexo_id",
                                "tiposangre_id",
                                "persona_documento",
                                "persona_prinombre",
                                "persona_segnombre",
                                "persona_priapellido",
                                "persona_segapellido",
                                "persona_fechanacimiento",
                                "rh_id"}
                Dim arrValor() As String = {
                                                Trim(Me.txtCodigo.Text),
                                                Trim(Me.cmbTipodocumento.SelectedValue.ToString),
                                                Trim(Me.cmbCiudad.SelectedValue.ToString),
                                                Trim(Me.cmbSexo.SelectedValue.ToString),
                                                Trim(Me.cmbTiposangre.SelectedValue.ToString),
                                                Trim(Me.txtCedula.Text),
                                                Trim(Me.txtPnombre.Text),
                                                Trim(Me.txtSnombre.Text),
                                                Trim(Me.txtPapellido.Text),
                                                Trim(Me.txtSapellido.Text),
                                                Trim(Me.dtpFechanacimiento.Text),
                                                Trim(Me.cmbrh.SelectedValue.ToString)
                                            }

                mdlCRUD.insRecord("persona", arrCampos, arrValor)
            Case 2 'Consultar

            Case 3 'Editar
                Dim arrParametro() As String = {
                                                "persona_id = " & IIf(Trim(Me.txtCodigo.Text) <> "", "'" & Me.txtCodigo.Text & "'", "null"),
                                                "persona_documento = " & IIf(Trim(Me.txtCedula.Text) <> "", "'" & Me.txtCedula.Text & "'", "null"),
                                                "persona_segnombre = " & IIf(Trim(Me.txtSnombre.Text) <> "", "'" & Me.txtSnombre.Text & "'", "null"),
                                                "persona_prinombre = " & IIf(Trim(Me.txtPnombre.Text) <> "", "'" & Me.txtPnombre.Text & "'", "null"),
                                                "persona_priapellido = " & IIf(Trim(Me.txtPapellido.Text) <> "", "'" & Me.txtPapellido.Text & "'", "null"),
                                                "persona_segapellido = " & IIf(Trim(Me.txtSapellido.Text) <> "", "'" & Me.txtSapellido.Text & "'", "null"),
                                                "persona_fechanacimiento = " & IIf(Trim(Me.dtpFechanacimiento.Text) <> "", "'" & Me.dtpFechanacimiento.Text & "'", "null"),
                                                "persona_lugarnacimiento = " & IIf(Trim(Me.cmbCiudad.SelectedValue.ToString) <> "", "'" & Me.cmbCiudad.SelectedValue.ToString & "'", "null"),
                                                "sexo_id = " & IIf(Trim(Me.cmbSexo.SelectedValue.ToString) <> "", "'" & Me.cmbSexo.SelectedValue.ToString & "'", "null"),
                                                "tipodocumento_id = " & IIf(Trim(Me.cmbTipodocumento.SelectedValue.ToString) <> "", "'" & Me.cmbTipodocumento.SelectedValue.ToString & "'", "null"),
                                                "tiposangre_id = " & IIf(Trim(Me.cmbTiposangre.SelectedValue.ToString) <> "", "'" & Me.cmbTiposangre.SelectedValue.ToString & "'", "null")
                                               }

                Dim strCondicion As String = "persona_id = " & IIf(Me.lblEdCodigo.Text <> "", "'" & Me.lblEdCodigo.Text & "'", "null")

                mdlCRUD.updRecordnomsg("persona", arrParametro, strCondicion)
        End Select

        If (mdlCRUD.blnFlag = True) Then
            Me.frmParent.refreshList()
            Me.Close()
        End If

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancelar.Click
        Select Case Me.modeForm
            Case 1 'Nuevo

            Case 2 'Consultar
                Me.frmParent.arrConsulta(0) = Trim(Me.txtCodigo.Text)
                Me.frmParent.arrConsulta(6) = Trim(Me.txtCedula.Text)
                Me.frmParent.arrConsulta(7) = Trim(Me.txtPnombre.Text)
                Me.frmParent.arrConsulta(8) = Trim(Me.txtSnombre.Text)
                Me.frmParent.arrConsulta(9) = Trim(Me.txtPapellido.Text)
                Me.frmParent.arrConsulta(10) = Trim(Me.txtSapellido.Text)

                Me.frmParent.arrConsulta(15) = Trim(Me.dtpFechanacimiento.Text)

                If Me.frmParent.arrConsulta(1) <> "" Then
                    Me.frmParent.arrConsulta(1) = Trim(Me.cmbTipodocumento.SelectedValue.ToString)
                End If
                If Me.frmParent.arrConsulta(3) <> "" Then
                    Me.frmParent.arrConsulta(3) = Trim(Me.cmbCiudad.SelectedValue.ToString)
                End If
                If Me.frmParent.arrConsulta(4) <> "" Then
                    Me.frmParent.arrConsulta(4) = Trim(Me.cmbSexo.SelectedValue.ToString)
                End If
                If Me.frmParent.arrConsulta(5) <> "" Then
                    Me.frmParent.arrConsulta(5) = Trim(Me.cmbTiposangre.SelectedValue.ToString)
                End If
                Me.frmParent.refreshList()

            Case 3 'Editar

        End Select


        Me.Close()
    End Sub
End Class